package controllers.account;

import static play.data.Form.form;

import java.util.ArrayList;
import java.util.List;

import models.Actor;
import models.Actor.TYPE;
import play.data.DynamicForm;
import play.data.Form;
import play.mvc.Result;
import repository.ActorRepository;
import service.ActorService;
import service.impl.ActorServiceImpl;
import views.html.register;
import web.entity.User;
import web.util.ResultCode;
import controllers.BaseController;
public class Register extends BaseController {
    final static Form<User> regForm = form(User.class);

    public static Result index() {
        return ok(register.render());
    }

    //@BodyParser.Of(BodyParser.Json.class)
    public static Result submit() {
    	ResultCode.ajaxResult ajaxResult=null;
        DynamicForm requestData = form().bindFromRequest();
        String email = requestData.get("inputEmail");
        String psw =   requestData.get("inputPassword");
        String cellPhone = requestData.get("inputPhone");
        ActorService actor = ActorRepository.getActorServiceByEmail(email);
        List<String> error=new ArrayList<String>();
        if(actor!=null){
        	ajaxResult=ResultCode.ajaxResult.ERROR;
        	
        	error.add("该邮箱已被注册，请输入新的邮箱重新再试！");
        	ajaxResult.setErrorMessage(error);
            return ok(ajaxResult.toJson());
        }
        actor = new ActorServiceImpl();
        try {
             actor.getProcessor().createActor(email, psw,cellPhone
             ,Actor.TYPE.CUSTOMER);
            actor.getProcessor().createActor(email, psw, cellPhone, TYPE.CUSTOMER);
        } catch (Throwable e) {
        	ajaxResult=ResultCode.ajaxResult.ERROR;
        	error.add(e.toString());
        	ajaxResult.setErrorMessage(error);
            return ok(ajaxResult.toJson());
        }
        ajaxResult=ResultCode.ajaxResult.SUCCESS;
        return ok(ajaxResult.toJson());
    }
}
